package com.crt.openapi.modules.apiinterface.dao.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.PropertyUtilsBean;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.crt.openapi.common.BaseDaoImpl;
import com.crt.openapi.common.BaseEntity;
import com.crt.openapi.modules.apiinterface.dao.ApiAppInfDao;
import com.crt.openapi.modules.apiinterface.dao.vo.ApiAppVO;
import com.crt.openapi.modules.common.domain.entity.ApiApp;

/**
 * ApiApp 基础Dao层接口实现
 * 
 * @Author LiuBao
 * @Version 2.0
 * @Date 2016年9月27日
 *
 */
@Repository("apiAppInfDao")
public class ApiAppInfDaoImpl extends BaseDaoImpl<ApiApp> implements ApiAppInfDao {

	public ApiAppInfDaoImpl() {
		super();
		mapperName = "ApiAppMapper";
	}

	@Override
	public List<ApiAppVO> selectListByPage(int pageIndex, int pageSize, ApiApp apiApp) {
		return this.selectListByPage(pageIndex, pageSize, apiApp, null);
	}

	@Override
	public int selectCountByPage(ApiApp apiApp) {
		return this.selectCountByPage(apiApp, null);
	}	
	
	@Override
	public List<ApiAppVO> selectListByPage(int pageIndex, int pageSize, ApiApp apiApp,String userId) {
		return getSqlSession().selectList(getNameSpace("selectListByPage"), describe(apiApp,userId),makeRowBounds(pageIndex,pageSize));
	}
	
	@Override
	public int selectCountByPage(ApiApp apiApp,String userId) {
		return getSqlSession().selectOne(getNameSpace("selectCountByPage"),describe(apiApp,userId));
	}
	
	
	@Override
	public List<ApiAppVO> selectAllListByPage(int pageIndex, int pageSize,
			ApiApp apiApp) {
		return getSqlSession().selectList(getNameSpace("selectAllListByPage"), apiApp,makeRowBounds(pageIndex,pageSize));
	}

	@Override
	public int selectAllCountByPage(ApiApp apiApp) {
		return getSqlSession().selectOne(getNameSpace("selectAllCountByPage"),apiApp);
	}
	
	@SuppressWarnings("rawtypes")
	@Override
	public Map apiAppDetail(int id) {
		// TODO Auto-generated method stub
		return getSqlSession().selectOne(getNameSpace("selectApiAppDetail"), id);
	}
	
	/**
	 * 获取命名空间
	 */
	protected String getNameSpace( Class<?> daoImplClass, String operate ) {
		if(daoImplClass==null){
			daoImplClass = this.getClass();
		}
		StringBuilder sb=new StringBuilder();
		sb.append(daoImplClass.getName().replaceAll("InfDaoImpl", "Mapper"));
		sb.append( "." + operate);
        return sb.toString();
    }
	
	protected String getNameSpace( String operate ) {
		Class<?> daoImplClass = this.getClass();
		return getNameSpace(daoImplClass, operate);
	}
	
	/**
	 * 请求参数封装为Map
	 */
	@SuppressWarnings("unchecked")
	public static <E extends BaseEntity>   Map<String, Object> describe( E entity , String ...paramStr ) {
		Map<String, Object> mapParam= new HashMap<>();
		mapParam=parseParamStr(mapParam,paramStr);
	    PropertyUtilsBean propertyUtils = BeanUtilsBean.getInstance().getPropertyUtils();
	    Map<String, Object> map;
		try {
			map = propertyUtils.describe( entity );
			if( map.size() > 0 ) {
				for( String key : map.keySet() ) {
					if( "class".equals( key ) ) {
						continue; 
					}
					Class<?> pClazz = propertyUtils.getPropertyType( entity, key );
					if( pClazz.isInterface()
							|| pClazz.isArray() 
							|| Collection.class.isAssignableFrom( pClazz )
							|| BaseEntity.class.isAssignableFrom( pClazz ) ) {
						continue;
					}
					Object value = map.get( key );
					if( pClazz.isEnum() && ( value != null ) ) {
						value = value.toString();
					}
					if(value != null&&StringUtils.isBlank(value.toString())){
						value=null;
					}
					mapParam.put(key, value);
				}
			}
			mapParam.put("delFlag", entity.isDelFlag());
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
	    return mapParam;
	}
	
	/**
	 * 添加非空请求参数信息
	 * 约定规则:[userId,,,,,]
	 */
	private static Map<String, Object> parseParamStr(Map<String, Object> mapParam, String ...paramStr) {
		if(MapUtils.isEmpty(mapParam)){
			mapParam= new HashMap<>();
		}
		if(paramStr!=null){
			switch (paramStr.length) {
			case 1:
				mapParam.put("userId", paramStr[0]);
				break;
			case 2:
				
				break;
			default:
				break;
			}
		}
		return mapParam;
	}

	@Override
	public List<ApiAppVO> selectAuthList(ApiApp apiApp,String userId) {
		return getSqlSession().selectList(getNameSpace("selectAuthList"), describe(apiApp,userId));
	}

	@Override
	public ApiApp selectLatestedOne(ApiApp apiApp) {
		return getSqlSession().selectOne(getNameSpace("selectLatestedOne"),apiApp);
	}

	@Override
	public List<ApiAppVO> selectListByFuzzy(ApiApp apiApp, String userId) {
		return getSqlSession().selectList(getNameSpace("selectListByFuzzy"), describe(apiApp,userId));
	}	
	
}
